Page({
  data: {
    cartList: [], //购物车信息
    totalPrice: 0, //总价格信息
    quantity: 0, //商品总数量信息
    datetime: null, //当前时间信息
    takeTime: null, //预计取餐时间信息
    takingId: null, //取餐号
    token: null //token令牌
  },

  onLoad() {
    //获取home页面缓存的购物车信息
    let cartList = wx.getStorageSync('cart')
    let totalPrice = wx.getStorageSync('totalPrice')
    let quantity = wx.getStorageSync('quantity')

    //将购物车信息、总价格和总数量信息赋值
    this.setData({
      cartList: cartList,
      totalPrice: totalPrice,
      quantity: quantity
    })

    //获取缓存的token令牌,并赋值
    let token = wx.getStorageSync('token')
    this.setData({
      token: token
    })
  },

  //跳转到支付成功页面
  goPaySuccess() {
    //保存this指针
    let that = this

    //获取当前时间,并赋值
    let year = new Date().getFullYear()
    let month = new Date().getMonth() + 1
    let day = new Date().getDate()
    let hour = new Date().getHours()
    let minute = new Date().getMinutes()
    let second = new Date().getSeconds()
    let datetime = year + '-' + this.addZero(month) + '-' + this.addZero(day) + ' ' + this.addZero(hour) + ':' + this.addZero(minute) + ':' + this.addZero(second);

    this.setData({
      datetime: datetime,
    })

    //存储当前时间
    wx.setStorageSync('datetime', datetime)

    //获取存储的user用户信息
    let userInfo = wx.getStorageSync('user')

    //判断用户是否登录
    if (userInfo != null) {
      //获取所需的购物车信息
      let itemRecords = []
      for (let i = 0; i < this.data.cartList.length; i++) {
        let commodityId = this.data.cartList[i].id
        let quantity = this.data.cartList[i].quantity
        itemRecords[i] = {
          commodityId: commodityId,
          itemQuantity: quantity
        }
      }
      console.log(itemRecords)

      //将订单信息发送至后端
      wx.request({
        url: 'http://localhost:8080/Orders/newOrder',
        method: 'POST',
        header: {
          'content-type': 'application/json', // 默认值
          'Authorization': "this.data.token"
        },

        //发送的数据信息
        data: {
          "orders": {
            "userId": userInfo.nickName,
            "totalPrice": this.data.totalPrice,
            "orderQuantity": this.data.quantity,
            "orderTime": this.data.datetime
          },
          "itemRecords": itemRecords
        },

        success(res) {
          that.setData({
            takingId: res.data.data.takingId,
            takeTime: res.data.data.takeTime
          })
        }
      })

      //设置定时器，发生异步事件
      setTimeout(this.goPay, 500)

    } else {
      //无用户登录信息，跳转至支付失败页面
      wx.navigateTo({
        url: '/pages/pay_fail/pay_fail',
      })
    }
  },

  goPay() {
    //缓存获取的取餐号和预计取餐时间
    wx.setStorageSync('takingId', this.data.takingId)
    wx.setStorageSync('takeTime', this.data.takeTime)

    //订单提交后清空购物车、总价格和总数量缓存
    wx.setStorageSync('cart', null)
    wx.setStorageSync('totalPrice', 0)
    wx.setStorageSync('quantity', 0)
    this.setData({
      cartList: [],
      totalPrice: 0,
      quantity: 0
    })

    //页面跳转
    wx.navigateTo({
      url: '/pages/pay_success/pay_success',
    })

    //微信自带接口，支付成功后订阅信息弹窗
    wx.requestSubscribeMessage({
      tmplIds: ['j0ql3lbKw66hByHfwUimtBKfT33K9Cm1948Kna18vhs', 'cVBSwbuEyBJ-tnpfsJIzjCB4WyxrwDINS8B696fBOKQ']
    })
  },

  //跳转到点餐页面
  goHome() {
    wx.reLaunch({
      url: '/pages/home/home',
    })
  },

  //判断获取时间参数是否小于10，若小于10在字符串前补0
  addZero(str) {
    return str < 10 ? ('0' + str) : str
  }

})